|
|
|
|
|
|
|
Figure 16.11
Program Light4 displaying a sphere with specular constant n = 100. |
|
|
|
|
|
|
|
|
normals as you generate the surface. When you create a sphere of radius R, for example, the unit normal vector at the point (x, y, z) on the sphere is <x / R, y / R, z / R>. As you create the polygons that represent the surface, you store these normals for later use. |
|
|
|
|
|
|
|
|
In other cases, you can approximate the normal vectors by taking the average of the normals of the surrounding polygons. Figure 16.12 shows four polygons meeting at a common vertex. The normal at the vertex is the average of the four polygon normals. |
|
|
|
|
|
|
|
|
Using the vertex normals and the lighting model equations, you can calculate the color values for each vertex. You can then use interpolation to find the colors for the other points inside the polygon. As the colors vary smoothly across the polygon, they produce a smoother image than you get if you give every point in the polygon the same color. For example, you can use this technique to create a smoother image of a sphere using fewer data points than you would need without Gouraud shading. |
|
|
|
|
|
|
|
|
Unfortunately, interpolating for each point in the polygon is slow and complicated. In fact, just listing the points is tricky. You cannot use the Polygon API function to draw the polygon because the interior points are not all the same color. |
|
|
|
|
|
|
|
|
One way to identify the pixels in the polygon is to use the CreatePolygonRgn API function to create a region to represent the polygon. Next, use the GetRgnBox function to get the minimum and maximum X and Y values in the region. Then loop over all these points and use the PtInRegion function to determine whether the point is inside the region. |
|
|
|
|
|
|
|
|
For each of the points in the region, take a weighted average of the colors of the polygon's vertexes. Even doing that can be difficult. Interpolation is straightforward if the region is a rectangle, but for other shapes, it is not as easy. Depending on the type of data you are drawing, the polygon may be a triangle, quadrilateral, or some other more strangely shaped area. |
|
|
|
|
|